Trò chơi giống như Rogue 2D sử dụng Unity
- BoardManager.cs
- Scripts /
- _Complete-Game /
- Assets /
- project /
2 using System;
3 using System.Collections.Generic; //Allows us to use Lists.
4 using Random = UnityEngine.Random; //Tells Random to use the Unity Engine random number generator.
5
6 namespace Completed
7
8 {
9
10 public class BoardManager : MonoBehaviour
11 {
12 // Using Serializable allows us to embed a class with sub properties in the inspector.
13 [Serializable]
14 public class Count
15 {
16 public int minimum; //Minimum value for our Count class.
17 public int maximum; //Maximum value for our Count class.
18
19
20 //Assignment constructor.
21 public Count (int min, int max)
22 {
23 minimum = min;
24 maximum = max;
25 }
26 }
27
28
29 public int columns = 8; //Number of columns in our game board.
30 public int rows = 8; //Number of rows in our game board.
31 public Count wallCount = new Count (5, 9); //Lower and upper limit for our random number of walls per level.
32 public Count foodCount = new Count (1, 5); //Lower and upper limit for our random number of food items per level.
33 public GameObject exit; //Prefab to spawn for exit.
34 public GameObject[] floorTiles; //Array of floor prefabs.
35 public GameObject[] wallTiles; //Array of wall prefabs.
36 public GameObject[] foodTiles; //Array of food prefabs.
37 public GameObject[] enemyTiles; //Array of enemy prefabs.
38 public GameObject[] outerWallTiles; //Array of outer tile prefabs.
39
40 private Transform boardHolder; //A variable to store a reference to the transform of our Board object.
41 private List <Vector3> gridPositions = new List <Vector3> (); //A list of possible locations to place tiles.
42
43
44 //Clears our list gridPositions and prepares it to generate a new board.
45 void InitialiseList ()
46 {
47 //Clear our list gridPositions.
48 gridPositions.Clear ();
49
50 //Loop through x axis (columns).
51 for(int x = 1; x < columns-1; x++)
52 {
53 //Within each column, loop through y axis (rows).
54 for(int y = 1; y < rows-1; y++)
55 {
56 //At each index add a new Vector3 to our list with the x and y coordinates of that position.
57 gridPositions.Add (new Vector3(x, y, 0f));
58 }
59 }
60 }
61
62
63 //Sets up the outer walls and floor (background) of the game board.
64 void BoardSetup ()
65 {
66 //Instantiate Board and set boardHolder to its transform.
67 boardHolder = new GameObject ("Board").transform;
68
69 //Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles.
70 for(int x = -1; x < columns + 1; x++)
71 {
72 //Loop along y axis, starting from -1 to place floor or outerwall tiles.
73 for(int y = -1; y < rows + 1; y++)
74 {
75 //Choose a random tile from our array of floor tile prefabs and prepare to instantiate it.
76 GameObject toInstantiate = floorTiles[Random.Range (0,floorTiles.Length)];
77
78 //Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles.
79 if(x == -1 || x == columns || y == -1 || y == rows)
80 toInstantiate = outerWallTiles [Random.Range (0, outerWallTiles.Length)];
81
82 //Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject.
83 GameObject instance =
84 Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
85
86 //Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy.
87 instance.transform.SetParent (boardHolder);
88 }
89 }
90 }
91
92
93 //RandomPosition returns a random position from our list gridPositions.
94 Vector3 RandomPosition ()
95 {
96 //Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
97 int randomIndex = Random.Range (0, gridPositions.Count);
98
99 //Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
100 Vector3 randomPosition = gridPositions[randomIndex];
101
102 //Remove the entry at randomIndex from the list so that it can't be re-used.
103 gridPositions.RemoveAt (randomIndex);
104
105 //Return the randomly selected Vector3 position.
106 return randomPosition;
107 }
108
109
110 //LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
111 void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
112 {
113 //Choose a random number of objects to instantiate within the minimum and maximum limits
114 int objectCount = Random.Range (minimum, maximum+1);
115
116 //Instantiate objects until the randomly chosen limit objectCount is reached
117 for(int i = 0; i < objectCount; i++)
118 {
119 //Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
120 Vector3 randomPosition = RandomPosition();
121
122 //Choose a random tile from tileArray and assign it to tileChoice
123 GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
124
125 //Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
126 Instantiate(tileChoice, randomPosition, Quaternion.identity);
127 }
128 }
129
130
131 //SetupScene initializes our level and calls the previous functions to lay out the game board
132 public void SetupScene (int level)
133 {
134 //Creates the outer walls and floor.
135 BoardSetup ();
136
137 //Reset our list of gridpositions.
138 InitialiseList ();
139
140 //Instantiate a random number of wall tiles based on minimum and maximum, at randomized positions.
141 LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
142
143 //Instantiate a random number of food tiles based on minimum and maximum, at randomized positions.
144 LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);
145
146 //Determine number of enemies based on current level number, based on a logarithmic progression
147 int enemyCount = (int)Mathf.Log(level, 2f);
148
149 //Instantiate a random number of enemies based on minimum and maximum, at randomized positions.
150 LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);
151
152 //Instantiate the exit tile in the upper right hand corner of our game board
153 Instantiate (exit, new Vector3 (columns - 1, rows - 1, 0f), Quaternion.identity);
154 }
155 }
156 }
using System.Collections.Generic; Allows us to use Lists.
using Random = UnityEngine.Random; Tells Random to use the Unity Engine random number generator.
Using Serializable allows us to embed a class with sub properties in the inspector.
public int minimum; Minimum value for our Count class.
public int maximum; Maximum value for our Count class.
Assignment constructor.
public int columns = 8; Number of columns in our game board.
public int rows = 8; Number of rows in our game board.
public Count wallCount = new Count (5, 9); Lower and upper limit for our random number of walls per level.
public Count foodCount = new Count (1, 5); Lower and upper limit for our random number of food items per level.
public GameObject exit; Prefab to spawn for exit.
public GameObject[] floorTiles; Array of floor prefabs.
public GameObject[] wallTiles; Array of wall prefabs.
public GameObject[] foodTiles; Array of food prefabs.
public GameObject[] enemyTiles; Array of enemy prefabs.
public GameObject[] outerWallTiles; Array of outer tile prefabs.
private Transform boardHolder; A variable to store a reference to the transform of our Board object.
private List
Clears our list gridPositions and prepares it to generate a new board.
Clear our list gridPositions.
Loop through x axis (columns).
Within each column, loop through y axis (rows).
At each index add a new Vector3 to our list with the x and y coordinates of that position.
Sets up the outer walls and floor (background) of the game board.
Instantiate Board and set boardHolder to its transform.
Loop along x axis, starting from -1 (to fill corner) with floor or outerwall edge tiles.
Loop along y axis, starting from -1 to place floor or outerwall tiles.
Choose a random tile from our array of floor tile prefabs and prepare to instantiate it.
Check if we current position is at board edge, if so choose a random outer wall prefab from our array of outer wall tiles.
Instantiate the GameObject instance using the prefab chosen for toInstantiate at the Vector3 corresponding to current grid position in loop, cast it to GameObject.
Set the parent of our newly instantiated object instance to boardHolder, this is just organizational to avoid cluttering hierarchy.
RandomPosition returns a random position from our list gridPositions.
Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
Remove the entry at randomIndex from the list so that it can't be re-used.
Return the randomly selected Vector3 position.
LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
Choose a random number of objects to instantiate within the minimum and maximum limits
Instantiate objects until the randomly chosen limit objectCount is reached
Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
Choose a random tile from tileArray and assign it to tileChoice
Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
SetupScene initializes our level and calls the previous functions to lay out the game board
Creates the outer walls and floor.
Reset our list of gridpositions.
Instantiate a random number of wall tiles based on minimum and maximum, at randomized positions.
Instantiate a random number of food tiles based on minimum and maximum, at randomized positions.
Determine number of enemies based on current level number, based on a logarithmic progression
Instantiate a random number of enemies based on minimum and maximum, at randomized positions.
Instantiate the exit tile in the upper right hand corner of our game board